package cn.pzaily.fealetter.platform;

import cn.pzaily.fealetter.entity.PlatFormConfig;
import cn.pzaily.fealetter.entity.SignatureEntity;
import com.alibaba.fastjson.JSON;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @PackageName: cn.pzaily.fealetter.platform
 * @Description:
 * @Author: Pang
 * @Date: 2021-11-23 19:42
 */
public class AliyunPlatFormService extends AbstractPlatFormService{


    private Config config;

    public AliyunPlatFormService(PlatFormConfig platFormConfig) {
        this.platFormConfig = platFormConfig;
        init();
    }


    private void init() {
        config = new Config()
                .setAccessKeyId(this.platFormConfig.getAccessKeyId())
                .setAccessKeySecret(this.platFormConfig.getAccessKeySecret())
                .setEndpoint("dysmsapi.aliyuncs.com");
    }


    @Override
    public String sendMessage(String mobile, Map<String, String> params, String template, String signature) {
        SignatureEntity signatureByCode = signatureService.findSignatureByCode(signature);

        try {
            Client client = new Client(config);
            SendSmsRequest request = new SendSmsRequest();
            request.setPhoneNumbers(mobile);
            request.setSignName(signature);
            request.setTemplateCode(template);
            request.setTemplateParam(JSON.toJSONString(params));

            SendSmsResponse response = client.sendSms(request);

            if ("OK".equals(response.getBody().getCode())) {
                return response.getBody().getMessage();
            } else {

            }


        } catch (Exception e) {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    public String sendBatchMessage(String[] mobile, LinkedHashMap<String, String> params, String template, String signature) {
        return null;
    }


}
